<?php
header( 'Content-Type:application/json; charset=utf-8' );
session_start();
if ( empty( $_SESSION[ 'userinfo' ] ) ) {
    $result = [
        'code' => '403',
        'msg' => '未登录，请登录',
    ];
    echo json_encode( $result );
    exit ;
}

$input = file_get_contents( 'php://input' );
$arr = json_decode( $input, true );
$title = $arr[ 'title' ] ?? '';
$content = $arr[ 'content' ] ?? '';
$startime = $arr[ 'startime' ] ?? '';
$endtime = $arr[ 'endtime' ] ?? '';
$work_img1 = $arr[ 'work_img1' ] ?? '';
$work_img2 = $arr[ 'work_img2' ] ?? '';
$work_img3 = $arr[ 'work_img3' ] ?? '';
$tch_id = $arr[ 'tch_id' ] ?? '';
$cou_id = $arr[ 'cou_id' ] ?? -1;

$dbms = 'mysql';
$host = '127.0.0.1';
$dbName = 'g09short';
$user = 'root';
$pass = 'G09short.';
$dsn = "$dbms:host=$host;dbname=$dbName";

$sql = "INSERT INTO zyh_work (`work_title`, `work_content`, `work_creatime`, `work_startime`, `work_endtime`, `work_img1`, `work_img2`, `work_img3`, `tch_id`, `cou_id`) VALUE('" . $title . "','" . $content . "', NOW() ,'" . $startime . "','" . $endtime . "','". $work_img1 . "','". $work_img2 . "','". $work_img3 . "','" . $tch_id . "','" . $cou_id . "')";

try {
    $dbh = new PDO( $dsn, $user, $pass );
    $stmt = $dbh->exec( $sql );
    if ( $stmt ) {
        $result = [
            'code' => '200',
            'msg' => '插入成功！',
            'data' => $stmt
        ];
    } else {
        $result = [
            'code' => '500',
            'msg' => '插入失败！',
            'data' => $work_img1
        ];
    }

    $dbh = null;

    echo json_encode( $result );
} catch ( PDOException $e ) {
    $result = [
        'code' => '500',
        'msg' => '$e->getMessage()',
    ];
    die( $result );
}
?>